package com.mgwt.phaseone.client.activities;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.IsWidget;
import com.google.gwt.user.client.ui.Widget;
import com.googlecode.mgwt.ui.client.dialog.AlertDialog;
import com.googlecode.mgwt.ui.client.widget.Button;
import com.googlecode.mgwt.ui.client.widget.MPasswordTextBox;
import com.googlecode.mgwt.ui.client.widget.MTextBox;
import com.mgwt.phaseone.client.RPCServiceAsync;
import com.mgwt.phaseone.client.RPCService;

;

public class LoginWidget extends FlowPanel implements IsWidget {

	private final RPCServiceAsync rpcService = GWT.create(RPCService.class);

	private ClickHandler clickHandler = new ClickHandler() {

		@Override
		public void onClick(ClickEvent event) {
			rpcService.isValidUser(user.getText(), pass.getText(),
					new AsyncCallback<Boolean>() {

						@Override
						public void onSuccess(Boolean result) {
							new AlertDialog("User Validation",
									result ? "Valid User" : "InvValid User")
									.show();

						}

						@Override
						public void onFailure(Throwable caught) {
							new AlertDialog("onFailure ERROR",
									caught.getLocalizedMessage())
									.show();

						}
					});
		}
	};;;

	@Override
	public Widget asWidget() {
		return this;
	}

	MTextBox user = new MTextBox();
	MPasswordTextBox pass = new MPasswordTextBox();

	public LoginWidget() {
		super();
		addStyleName("login-widget");

		Button submit = new Button("Submit");
		submit.addDomHandler(clickHandler, ClickEvent.getType());

		add(user);
		add(pass);
		add(submit);

	}

}
